package net.sf.nwn.jme3;


import java.util.LinkedHashMap;
import java.util.Map;

import com.jme3.asset.AssetManager;


public class ModelPrototype
{
    private String name;
    private String supermodelName;
    private float animationScale;
    private String classification;
    private GeomNode modelGeometry;
    private LinkedHashMap<String,ModelAnimation> animations = new LinkedHashMap<String,ModelAnimation>();

    public ModelPrototype(String aName)
    {
        name = aName.toLowerCase();
    }

    public String getName()
    {
        return name;
    }

    /**
     * Sets the name.
     * @param name The name to set
     */
    public void setName(String name)
    {
        this.name = name.toLowerCase();
    }

    /**
     * Gets the animationScale.
     * @return Returns a float
     */
    public float getAnimationScale()
    {
        return animationScale;
    }

    /**
     * Sets the animationScale.
     * @param animationScale The animationScale to set
     */
    public void setAnimationScale(float animationScale)
    {
        this.animationScale = animationScale;
    }

    /**
     * Gets the classification.
     * @return Returns a String
     */
    public String getClassification()
    {
        return classification;
    }

    /**
     * Sets the classification.
     * @param classification The classification to set
     */
    public void setClassification(String classification)
    {
        this.classification = classification;
    }

    /**
     * Gets the supermodelName.
     * @return Returns a String
     */
    public String getSupermodelName()
    {
        return supermodelName;
    }

    /**
     * Sets the supermodelName.
     * @param supermodelName The supermodelName to set
     */
    public void setSupermodelName(String supermodelName)
    {
        this.supermodelName = supermodelName;
    }

    public ModelPrototype getSupermodel(AssetManager manager)
    {
        if (getSupermodelName() == null || getSupermodelName().equalsIgnoreCase("null"))
            return null;
        
        String supermodelFile = getSupermodelName();
        
        if ( !supermodelFile.endsWith(".mdl")) {
        	supermodelFile += ".mdl";
        }

        ModelPrototype supermodel = (ModelPrototype) manager.loadAsset(supermodelFile);
        return supermodel;
    }

    /**
     * Gets the modelGeometry.
     * @return Returns a ModelGeometry
     */
    public GeomNode getModelGeometry()
    {
        return modelGeometry;
    }

    /**
     * Sets the modelGeometry.
     * @param modelGeometry The modelGeometry to set
     */
    public void setModelGeometry(GeomNode modelGeometry)
    {
        this.modelGeometry = modelGeometry;
    }

    public void addModelAnimation(ModelAnimation anim)
    {
        animations.put(anim.getName(), anim);
    }

    public String toString()
    {
        return "Model(" + getName() + ") \n" + modelGeometry +
            "\n" + animations.values();
    }

    public boolean isMetallic()
    {
        return (getSupermodelName() != null && !getSupermodelName().equalsIgnoreCase("null")) ||
            animations.size() > 0;
    }

    /*
     public javax.media.j3d.GeometryArray findGeometry(String name)
     {
     return ((TrimeshGeomNode) modelGeometry.findChild(name)).createGeometry();
     }


     public javax.media.j3d.Appearance findAppearance(URL base, String name)
     {
     return ((TrimeshGeomNode) modelGeometry.findChild(name)).createAppearance(base);
     }
     */
    public ModelAnimation findAnimation(String name)
    {
        return (ModelAnimation) animations.get(name);
    }

    public Map<String,ModelAnimation> getAllAnimations()
    {
        return animations;
    }

    public void dump(StringBuffer sb)
    {
        sb.append("#Dumped by java\n");
        sb.append("newmodel ").append(getName()).append("\n");
        sb.append("setsupermodel ").append(getName()).append(" ").append(getSupermodelName()).append("\n");
        sb.append("classification ").append(getClassification()).append("\n");
        sb.append("setanimationscale ").append(getAnimationScale()).append("\n\n");
        sb.append("beginmodelgeom ").append(getName()).append("\n");
        modelGeometry.dump(sb);
        sb.append("endmodelgeom ").append(getName()).append("\n\n");
        
        for ( ModelAnimation ma : animations.values() ) {
        	ma.dump(sb);
        }
        
        sb.append("donemodel ").append(getName()).append("\n");
    }

    public void fixupEmitters()
    {
    	for ( ModelAnimation ma : animations.values() ) {
    		ma.fixupEmitters(modelGeometry);
    	}
    }

}
